package com.techstar.ats.netty.handler;

import java.util.HashMap;
import java.util.Map;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import com.techstar.ats.MessageType;
import com.techstar.ats.dxp.history.AbstractReport;
import com.techstar.ats.dxp.history.req.F12ReportAsk;

public class ReportHandler extends ChannelDuplexHandler {
	public final Map<Integer, F12ReportAsk> reportMsgs = new HashMap<>();
	private int report = 0;

	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		if(msg instanceof AbstractReport) {
			AbstractReport ar = (AbstractReport) msg;

			try {
				MessageType messageType = MessageType.valueOf(ar.id);
				switch(messageType) {
					case GROUP_RUNNING_REPORT:
					case DRIVER_DISTANCE_REPORT:
					case DISPATCHER_REPORT:
					case GROUP_BAK_REPORT:
					case GROUP_STATUS_REPORT:
				}
			} finally {
				if(MessageType.REPORT_END.value() == ar.id || MessageType.REPORT_NACK.value() == ar.id) {
					this.reportMsgs.remove(ar.report);
				}

			}
		}

		super.channelRead(ctx, msg);
	}

	public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
		if(msg instanceof F12ReportAsk) {
			((F12ReportAsk) msg).report = ++this.report;
			this.reportMsgs.put(this.report, (F12ReportAsk) msg);
		}

		super.write(ctx, msg, promise);
	}
}
